﻿using System;
using System.Collections.Generic;
using GameEditor.View.Components;
using GameEngine.Model.Data;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using GameEngine.Model;
using System.Windows;
using GameEngine.View;
using GameEngine;

namespace GameEditor.View
{

    public class ThingPropertiesEditor_MenuMediator<IdType> : MenuMediator<IThingEditorMenuViewConstants.ButtonTags, IThingEditorMenuView<IdType>>
        where IdType : IComparable<IdType>
    {
        //public readonly String ADDITEMTOGAME_NOTIFICATION = "ItemEditorMediator_ADDITEMTOGAME_";
        //public readonly String CREATEITEM_BUTTONPRESSED = "ItemEditorMenuMediator_CREATEITEM_";
        //public readonly String ADDITEM_BUTTONPRESSED = "ItemEditorMenuMediator_ADDITEM_";

        //IThingTypeDataProxy<ItemVOId> OnBuildThingType;
        IThingDataProxy<IdType> proxy;
        public IThingDataProxy<IdType> DataProxy
        {
            get { return proxy; }
            set { 
                proxy = value;
                if (proxy == null)
                    ViewComponent.EnableControl = false;
                else
                {
                    ViewComponent.Position = proxy.Position;
                    //ViewComponent.Type = proxy.TypeId;
                    ViewComponent.EnableControl = true;
                }
            }
        }

        public ThingPropertiesEditor_MenuMediator(String name, IThingEditorMenuView<IdType> viewComponent, IThingDataProxy<IdType> proxy, string outCommand)
            : base(name, viewComponent,outCommand)
        {
            this.DataProxy = proxy;
            //CREATEITEM_BUTTONPRESSED += name;
            //ADDITEM_BUTTONPRESSED += name;
            //ADDSHAPE_BUTTONPRESSED += name;
            //REMOVESHAPE_BUTTONPRESSED += name;
        }

        public new IThingEditorMenuView<IdType> ViewComponent
        {
            get { return base.ViewComponent as IThingEditorMenuView<IdType>; }
        }

        public Point Position {
            get {
                return ViewComponent.Position;
            }
            set {
                ViewComponent.Position = value;
            }
        }

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            IList<string> n = base.ListNotificationInterests();
            n.Add(MapMediatorConstants<IdType>.ITEMSELECTED);
            n.Add(IThingMediatorConstants<IdType>.ITEMMOUSEUP);
            n.Add(IThingMediatorConstants<IdType>.ITEMMOUSEDOWN);
            return n;
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                case MapMediatorConstants<IdType>.ITEMSELECTED:
                    {
                        var thingDataProxy = notification.Body as IThingDataProxy<IdType>;
                        DataProxy = thingDataProxy;
                        //setmouse move
                    }
                    break;
                case IThingMediatorConstants<IdType>.ITEMMOUSEUP:
                    //set unomve
                    break;
                case IThingMediatorConstants<IdType>.ITEMMOUSEDOWN:
                    {
                        if (DataProxy != null)
                        {
                            var thingDataProxy = notification.Body as IThingDataProxy<IdType>;
                            if (DataProxy.Id.Value.Equals(thingDataProxy.Id.Value))
                            {
                                ApplicationFacadeHelper<IdType>.ControlThingPosition(DataProxy.Id);
                            }
                        }
                    }
                    break;
            }
            base.HandleNotification(notification);
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            base.OnRegister();
            //ViewComponent.OnButtonClick +=new GameEngine.View.Components.OnButtonClickHandler<IThingEditorMenuViewConstants.ButtonTags>(ViewComponent_OnButtonClick);
            //ViewComponent.OnAddItemToWorldClick += new Action<IThingTypeVOId<IdType>>(ViewComponent_OnAddItemClick);
            //ViewComponent.OnCreateClick += new Action<IThingTypeVOId>(ViewComponent_OnCreateItemClick);
            //ViewComponent.OnItemTypeClick += new Action<string>(ViewComponent_OnItemTypeClick);
            //ViewComponent.OnNewClick += new Action(ViewComponent_OnNewClick);
            //ViewComponent.OnEditItemClick += new Action<string, string>(ViewComponent_OnEditItemClick);
        }

/*void  ViewComponent_OnButtonClick(IThingEditorMenuViewConstants.ButtonTags text)
{
 	SendNotification(GameEditorFacade.THINGEDITORMENU_ACTION_COMMAND,text);
}
        */
        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
            base.OnRemove();
        }

        #endregion

        //void ViewComponent_OnAddItemClick(IThingTypeVOId<IdType> id)
        //{
        //    //TODO SendNotification(
        //    //    ADDITEMTOGAME_NOTIFICATION,
        //    //    ModelFacade<ItemVOId>.Instance.CreateThingDataProxy(
        //    //        ModelFacade<ItemVOId>.Instance.Engine.Game.ThingTypes[id]
        //    //    ),
        //    //    null);
        //    //SendNotification(ADDITEM_BUTTONPRESSED, null, obj);
        //    //WorldProxy.AddItem(
        //    //    ModelFacade<ItemVOId>.Instance.CreateItemDataProxy(
        //    //        WorldProxy.ItemTypes[obj]
        //    //    ));
        //}

        //void ViewComponent_OnNewClick()
        //{
        //    //ItemEditorWorldMediator.proxy.RemoveAllItems();
        //    //ViewComponent.RemoveAllShapesFromList();
        //}

        //void ViewComponent_OnItemTypeClick(string obj)
        //{
        //    //TODO adicionar type ao itemEditorWorldMediator 
        //    //IThingTypeDataProxy<ItemVOId> itemType = GameWorldProxy.ItemTypes[obj];
        //    //ItemViewerWorldMediator.proxy.RemoveAllItems();
        //    //IThingDataProxy<ItemVOId> item = ModelFacade<ItemVOId>.Instance.CreateItemDataProxy(itemType);
        //    //ItemViewerWorldMediator.proxy.AddItem(item);
        //}
    }
}